var vm=new Vue({
    el:"#app",
    data:{
        totalMoney:0,
        productList:[],
        checkAllFlag:false,
        delFlag:false,
        curProduct:''
    },
    filters:{
        formatMoney:function (value) {
            return "￥ "+value.toFixed(2);
        }
    },
    mounted:function () {
        this.$nextTick(function () {
            this.cartView() //也可以 vm.cartView()
        })
    },
    methods:{
        cartView:function () {
            var _this=this;
            //也可以 vm.$http
            this.$http.get("/data/json/cartData.json").then(function (res) {
                _this.productList=res.data.result.list;
                // _this.totalMoney=res.data.result.totalMoney;
            })
        },
        changeMoney:function (product,way) {
            if (way>0){
                product.productQuantity++;
            }else{
                product.productQuantity--;
                if(product.productQuantity<1){
                    product.productQuantity=1;
                }
            }
            this.calcTotalPrice();
        },
        selectProduct:function (item) {
            if (typeof  item.checked =="undefined"){
                // Vue.set(item,"checked",true); //全局注册
                this.$set(item,"checked",true); //局部注册
            }else {
                item.checked=!item.checked;
            }
            this.calcTotalPrice();
        },
        checkAll:function (flag) {
            this.checkAllFlag=flag;
            var _this=this;
            this.productList.forEach(function (item,index) {
                if (typeof  item.checked =="undefined"){
                    _this.$set(item,"checked",_this.checkAllFlag); //局部注册
                }else {
                    item.checked=_this.checkAllFlag;
                }
            });
            this.calcTotalPrice();
        },
        calcTotalPrice:function () {
            this.totalMoney=0;
            var _this=this;
            this.productList.forEach(function (item,index) {
              if (item.checked){
                  _this.totalMoney+=item.productPrice*item.productQuantity;
              }
            })
        },
        delConfirm:function (item) {
            this.delFlag=true;
            this.curProduct=item;
        },
        delProduct:function () {
            var index=this.productList.indexOf(this.curProduct);
            this.productList.splice(index,1);
            this.delFlag=false;
        }
    }
});
Vue.filter("money",function (value,type) {
    return "￥ "+value.toFixed(2) +type;
});